/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.opmviewer.prefuse;

import prefuse.action.layout.graph.NodeLinkTreeLayout;
import prefuse.data.Graph;
import prefuse.data.Tree;
import prefuse.data.tuple.TupleSet;
import prefuse.visual.NodeItem;

/**
 *
 * @author grzegorz
 */
public class MyNodeLinkTreeLayout extends NodeLinkTreeLayout {

    private NodeItem nextRootItem = null;

    public MyNodeLinkTreeLayout(String group, int i,
            double d, double d1, double d2, NodeItem firstRootItem) {
        super(group, i, d, d1, d2);
        setNextRootItem(firstRootItem);
    }

    public void setNextRootItem(NodeItem nextRootItem) {
        this.nextRootItem = nextRootItem;
    }
    
    @Override
    public NodeItem getLayoutRoot() {
        if (m_root != null) {
            return m_root;
        }

        TupleSet ts = m_vis.getGroup(m_group);
        if (ts instanceof Graph) {
            if (nextRootItem != null) {                                    // <--- the extension
                ((Graph) ts).getSpanningTree(nextRootItem);
                NodeItem ret = nextRootItem;
                nextRootItem = null;
                return ret;
            } else {                                                       // <--- regular case
                Tree tree = ((Graph) ts).getSpanningTree();
                return (NodeItem) tree.getRoot();
            }
        } else {
            throw new IllegalStateException("This action's data group does"
                    + "not resolve to a Graph instance.");
        }
    }
}
